package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.FeedbackQuestionBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by huangxiaojian on 2017/8/1.
 *
 */

public class FeedbackQurstionParser extends LVideoMobileParser<FeedbackQuestionBean>{

    @Override
    public FeedbackQuestionBean parse(JSONObject data) throws Exception {
        if (data == null) {
            return null;
        }

        FeedbackQuestionBean feedbackQuestionBean = new FeedbackQuestionBean();
        ArrayList<FeedbackQuestionBean.QuestionItem> questionList = new ArrayList<>();
        JSONArray category = data.getJSONArray("category");
        if (category == null || category.length() == 0) {
            return null;
        }

        JSONObject questionType;
        for (int i = 0; i < category.length(); i++) {
            FeedbackQuestionBean.QuestionItem questionItem = new FeedbackQuestionBean.QuestionItem();
            questionType = (JSONObject) category.get(i);
            if (questionItem == null) {
                continue;
            }
            questionItem.name = questionType.optString("name");
            questionItem.id = questionType.optString("id");

            JSONArray subQuestionArray = questionType.optJSONArray("subcategory");
            if (subQuestionArray == null || subQuestionArray.length() == 0) {
                questionList.add(questionItem);
                continue;
            }
            ArrayList<FeedbackQuestionBean.Subcategory> subQuestionList = new ArrayList<>();

            for (int m = 0; m < subQuestionArray.length(); m++) {
                FeedbackQuestionBean.Subcategory subQuestionItem = new FeedbackQuestionBean.Subcategory();
                JSONObject currentSubQuestionItem = subQuestionArray.getJSONObject(m);
                if (currentSubQuestionItem == null) {
                    continue;
                }
                subQuestionItem.name = currentSubQuestionItem.optString("name");
                subQuestionItem.id = currentSubQuestionItem.optString("id");
                subQuestionList.add(subQuestionItem);
            }
            questionItem.subcategory = subQuestionList;

            questionList.add(questionItem);
        }
        feedbackQuestionBean.category = questionList;
        return feedbackQuestionBean;
    }
}
